import useJudgeLogin from './judgeLogin'
import { reportSearch, reportSearchBattery, reportSearchWEIBAO } from '@/apis/report'
import router from '@/router'

interface QueryInterface {
    res?: any
    vin: string
    orderNo: string
    vinParseId: string
    reportType: number
    isSkip: boolean
    skipMode: string
    loop: boolean
    reportId?: string
    succeed: () => void
    failed: () => void
}

export default class QueryReportStatus {
    timer: any
    loop = true
    count = 0
    constructor() {}
    query({
        vin,
        orderNo,
        vinParseId,
        reportType,
        isSkip = true,
        skipMode = 'replace',
        loop = true,
        reportId = '',
        succeed = () => {},
        failed = () => {}
    }: QueryInterface) {
        this.loop = loop
        this.count = 0
        useJudgeLogin.init(() => {
            console.log('查询页面', vin, orderNo, vinParseId, reportId)
            if (reportType === 1) {
                //出险
                reportSearch(orderNo, vin, vinParseId).then(
                    (res: any) => {
                        this.resultDispose({
                            res,
                            vin,
                            orderNo,
                            vinParseId,
                            reportType,
                            isSkip,
                            skipMode,
                            loop,
                            succeed,
                            failed
                        })
                    },
                    () => {
                        failed()
                    }
                )
            } else if (reportType === 2) {
                //维保车况
                reportSearchWEIBAO(orderNo).then(
                    (res: any) => {
                        this.resultDispose({
                            res,
                            vin,
                            orderNo,
                            vinParseId,
                            reportType,
                            isSkip,
                            skipMode,
                            loop,
                            succeed,
                            failed
                        })
                    },
                    () => {
                        failed()
                    }
                )
            } else if (reportType === 3) {
                //新能源车况
                reportSearchBattery(orderNo, vin, vinParseId).then(
                    (res: any) => {
                        this.resultDispose({
                            res,
                            vin,
                            orderNo,
                            vinParseId,
                            reportType,
                            isSkip,
                            skipMode,
                            loop,
                            succeed,
                            failed
                        })
                    },
                    () => {
                        failed()
                    }
                )
            }
        })
    }
    resultDispose = ({
        res,
        vin,
        orderNo,
        vinParseId,
        reportType,
        isSkip = true,
        skipMode = 'replace',
        loop = true,
        succeed = () => {},
        failed = () => {}
    }: QueryInterface) => {
        if (res.data.value === 200) {
            //查询成功 报告状态（0.待生成；1.成功；2.失败；3.失效）
            if (res.data.data.reportStatus === 1) {
                //成功 100ms后跳转到报告详情
                this.timer = setTimeout(() => {
                    clearTimeout(this.timer)
                    this.count = 0
                    console.log(this)
                    if (isSkip) {
                        if (skipMode === 'replace') {
                            if (reportType === 1) {
                                //出险
                                router.replace({
                                    path: '/report-html',
                                    query: {
                                        reportId: res.data.data.id
                                    }
                                })
                                // router.replace({
                                //     path: '/collision',
                                //     query: {
                                //         reportId: res.data.data.id,
                                //         vinParseId: vinParseId,
                                //         productType: reportType
                                //     }
                                // })
                            } else if (reportType === 2) {
                                //维保车况
                                router.replace({
                                    path: '/webview',
                                    query: {
                                        url: res.data.data.maintenanceReportUrl,
                                        reportId: res.data.data.reportId
                                    }
                                })
                            } else if (reportType === 3) {
                                //新能源车况
                                router.replace({
                                    path: '/new-energy',
                                    query: {
                                        reportId: res.data.data.id,
                                        vinParseId: vinParseId,
                                        productType: reportType
                                    }
                                })
                            }
                        } else if (skipMode === 'push') {
                            if (reportType === 1) {
                                //出险
                                router.push({
                                    path: '/report-html',
                                    query: {
                                        reportId: res.data.data.id
                                    }
                                })
                                // router.push({
                                //     path: '/collision',
                                //     query: {
                                //         reportId: res.data.data.id,
                                //         vinParseId: vinParseId,
                                //         productType: reportType
                                //     }
                                // })
                            } else if (reportType === 2) {
                                //维保车况
                                router.push({
                                    path: '/webview',
                                    query: {
                                        url: res.data.data.maintenanceReportUrl,
                                        reportId: res.data.data.reportId
                                    }
                                })
                            } else if (reportType === 3) {
                                //新能源车况
                                router.push({
                                    path: '/new-energy',
                                    query: {
                                        reportId: res.data.data.id,
                                        vinParseId: vinParseId,
                                        productType: reportType
                                    }
                                })
                            }
                        }
                    }
                    succeed()
                }, 100)
            } else if (res.data.data.reportStatus == null || res.data.data.reportStatus === 0) {
                if (this.count <= 1) {
                    if (!this.loop) {
                        this.count++
                    }
                    //待生成；等待2s再次查询
                    this.timer = setTimeout(() => {
                        clearTimeout(this.timer)
                        if (reportType === 1) {
                            //出险
                            reportSearch(orderNo, vin, vinParseId).then(
                                (res: any) => {
                                    this.resultDispose({
                                        res,
                                        vin,
                                        orderNo,
                                        vinParseId,
                                        reportType,
                                        isSkip,
                                        skipMode,
                                        loop,
                                        succeed,
                                        failed
                                    })
                                },
                                () => {
                                    failed()
                                }
                            )
                        } else if (reportType === 3) {
                            //新能源车况
                            reportSearchBattery(orderNo, vin, vinParseId).then(
                                (res: any) => {
                                    this.resultDispose({
                                        res,
                                        vin,
                                        orderNo,
                                        vinParseId,
                                        reportType,
                                        isSkip,
                                        skipMode,
                                        loop,
                                        succeed,
                                        failed
                                    })
                                },
                                () => {
                                    failed()
                                }
                            )
                        }
                    }, 2000)
                } else {
                    this.timer = setTimeout(() => {
                        clearTimeout(this.timer)
                        this.count = 0
                        failed()
                    }, 2000)
                }
            } else {
                //不成功
                this.timer = setTimeout(() => {
                    clearTimeout(this.timer)
                    this.count = 0
                    failed()
                }, 2000)
            }
        } else {
            // CxlMessage.error(res.data.desc)
            //100ms 显示失败
            this.timer = setTimeout(() => {
                clearTimeout(this.timer)
                this.count = 0
                failed()
            }, 100)
        }
    }
    destroy() {
        if (this.timer) {
            clearTimeout(this.timer)
            this.count = 0
        }
    }
}
